<?php

namespace Kunfeng\Dify\Exception;

/**
 * 验证异常类
 */
class ValidationException extends DifyApiException
{
    private $field;
    private $value;

    public function __construct(
        string $message,
        string $field = '',
        $value = null,
        \Throwable $previous = null
    ) {
        parent::__construct($message, 400, [], $previous);
        $this->field = $field;
        $this->value = $value;
    }

    /**
     * 获取字段名
     */
    public function getField(): string
    {
        return $this->field;
    }

    /**
     * 获取字段值
     */
    public function getValue()
    {
        return $this->value;
    }

    /**
     * 创建字段验证异常
     */
    public static function forField(string $field, $value, string $message = ''): self
    {
        if (empty($message)) {
            $message = "Invalid value for field '{$field}'";
        }
        return new self($message, $field, $value);
    }

    /**
     * 创建必需字段异常
     */
    public static function requiredField(string $field): self
    {
        return new self("Field '{$field}' is required", $field, null);
    }
}
